package com.longcai.cm.base.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.longcai.cm.delay.OrderDelay;
import com.longcai.cm.delay.SecKillGoodDelay;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.concurrent.DelayQueue;

@Configuration
public class BaseConfig {


    @Getter
    public static String UeditorPrePath;

    @Value("${lcmaster.upload.prefix}")
    public void setPath(String db) {
        UeditorPrePath = db;
    }


    @Getter
    public static String UeditorPath;

    @Value("${lcmaster.upload.kindeditorUploadPath}")
    public void setDatabase(String db) {
        UeditorPath = db;
    }

    @Autowired
    private Environment env;

    private JdbcTemplate jdbcTemplate;

    /**
     * 页面代码生成器需要
     */
    @Bean(name = "oaJdbcTemplate")
    public JdbcTemplate dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        //用户名
        dataSource.setUsername(env.getProperty("spring.datasource.username"));
        //密码
        dataSource.setPassword(env.getProperty("spring.datasource.password"));
        dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setInitialSize(2);
        dataSource.setMaxActive(20);
        dataSource.setMinIdle(0);
        dataSource.setMaxWait(60000);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTestOnBorrow(false);
        dataSource.setTestWhileIdle(true);
        dataSource.setPoolPreparedStatements(false);
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        System.out.println("初始化jdbc");
        return jdbcTemplate;

    }

    @Bean
    public DelayQueue<SecKillGoodDelay> secKillGoodDelayQueue() {
        return new DelayQueue<>();
    }

    @Bean
    public DelayQueue<OrderDelay> orderDelayQueue() {
        return new DelayQueue<>();
    }

}
